//
//  Car_CarInfoTableViewCell.m
//  CarUser
//
//  Created by ayi on 2019/1/8.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "Car_CarInfoTableViewCell.h"

@interface Car_CarInfoTableViewCell()

@property (nonatomic, strong) UILabel *carNameLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *moneyLab;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *typeLabel1;
@property (nonatomic, strong) UIView *colorView;
//@property (nonatomic, strong) UILabel *leftLab;
//@property (nonatomic, strong) UILabel *rightLab;
@property (nonatomic, strong) UIView *functionView;

@end

@implementation Car_CarInfoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (Car_CarInfoTableViewCell *)initCellWithReuseIdentifier:(NSString *)reuseIdentifier andTab:(UITableView *)tab{
    Car_CarInfoTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[Car_CarInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _carNameLab = [UILabel customInitLabWithFont:18 andTextColor:UIColor.blackColor andAlign:NSTextAlignmentLeft];
        _carNameLab.font = B_FONT_SIZE(18);
        _timeLab = [UILabel customInitLabWithFont:12 andTextColor:Common_TextColor andAlign:NSTextAlignmentRight];
        _moneyLab = [UILabel customInitLabWithFont:20 andTextColor:UIColor.blackColor andAlign:NSTextAlignmentLeft];
        _moneyLab.font = B_FONT_SIZE(20);
        
//        _tipsView = [[UIView alloc]init];
//        _tipsLab = [UILabel customInitLabWithFont:12 andTextColor:Common_TextColor andAlign:NSTextAlignmentRight];
//        _tipsLab.numberOfLines = 0;
        _colorView = [[UIView alloc]init];_colorView.backgroundColor = Common_Head_Color;
        
//        _leftLab = [UILabel customInitLabWithFont:13 andTextColor:Common_TextColor andAlign:NSTextAlignmentLeft];
//        _leftLab.numberOfLines = 0;
//        _rightLab = [UILabel customInitLabWithFont:13 andTextColor:Common_TextColor andAlign:NSTextAlignmentLeft];
//        _rightLab.numberOfLines = 0;
        
        [self.contentView addSubview:_carNameLab];
        [self.contentView addSubview:_timeLab];
        [self.contentView addSubview:_moneyLab];
//        [self.contentView addSubview:_tipsView];
//        [self.contentView addSubview:_tipsLab];
        [self.contentView addSubview:self.typeLabel];
        [self.contentView addSubview:self.typeLabel1];
        [self.contentView addSubview:_colorView];
        [self.contentView addSubview:self.functionView];
//        [self.contentView addSubview:_leftLab];
//        [self.contentView addSubview:_rightLab];
        
        [_carNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.left.mas_equalTo(15);
            make.height.mas_equalTo(ceilf(self.carNameLab.font.lineHeight));
        }];
        [_carNameLab setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        
        [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.carNameLab.mas_bottom);
            make.right.mas_equalTo(-15);
            make.left.equalTo(self.carNameLab.mas_right).offset(8);
            make.height.mas_equalTo(ceilf(self.timeLab.font.lineHeight));
        }];
        [_timeLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        
//        [_tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.carNameLab.mas_bottom).offset(15);
//            make.left.mas_equalTo(15);
//            make.height.mas_greaterThanOrEqualTo(20);
//        }];
//        [_tipsLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        
        
        [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.carNameLab.mas_bottom).offset(30);
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.width.mas_equalTo(38);
            make.height.mas_equalTo(18);
        }];
        
        [self.typeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.carNameLab.mas_bottom).offset(30);
            make.left.equalTo(self.typeLabel.mas_right).offset(10);
            make.width.mas_equalTo(38);
            make.height.mas_equalTo(18);
        }];
        
        [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.typeLabel.mas_top);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(ceilf(self.moneyLab.font.lineHeight));
        }];
        [_moneyLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        
        [_colorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.typeLabel.mas_bottom).offset(15);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_greaterThanOrEqualTo(20);
            
//            make.bottom.mas_equalTo(-10);
        }];
        
//        [_leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.colorView.mas_bottom).offset(10);
//            make.left.mas_equalTo(15);
//            make.bottom.mas_equalTo(-10);
//            make.width.equalTo(self.rightLab.mas_width);
//        }];
//        [_leftLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
//
//        [_rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.leftLab.mas_top);
//            make.left.equalTo(self.leftLab.mas_right);
//            make.right.mas_equalTo(-15);
//        }];
//        [_rightLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
//
        
        
    }
    return self;
}
- (UIView *)functionView
{
    if (!_functionView) {
        _functionView = [UIView new];
//        _functionView.layer.cornerRadius = 5;
//        _functionView.layer.masksToBounds = YES;
    }
    return _functionView;
    
}


- (void)setModel:(CarDetailMainModel *)model{
    _model = model;
    
    self.carNameLab.text = model.cardetailInfoModel.CXMC00;
    self.timeLab.text = model.cardetailInfoModel.SJRQ00;
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@万",model.cardetailInfoModel.ZDJG00];
    
    //标签
    [self resetTipsLabText];
    //颜色
    [self resetColorView];
    //安全配置
    [self resetConfiguration];
    
}

//标签
- (void)resetTipsLabText{
    
    self.typeLabel.text = self.model.cardetailInfoModel.GHFSMC?:@"";
    self.typeLabel1.text = self.model.cardetailInfoModel.RLMC00?:@"";
    
//    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
//    [str appendAttributedString:[UIView imgAttirbuteStrWith:self.model.cardetailInfoModel.GHFSMC?:@""]];
//    [str appendAttributedString:[[NSMutableAttributedString alloc]initWithString:@"   "]];
//    [str appendAttributedString:[UIView imgAttirbuteStrWith:self.model.cardetailInfoModel.RLMC00?:@""]];
//
//    self.tipsLab.attributedText = str;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [UILabel new];
        _typeLabel.textColor = Common_Text_TextfieldColor;
        _typeLabel.textAlignment = NSTextAlignmentCenter;
        _typeLabel.font = FONT_SIZE(12);
        _typeLabel.layer.borderWidth = 1;
        _typeLabel.layer.cornerRadius = 6;
        _typeLabel.layer.masksToBounds = YES;
        _typeLabel.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel;
    
}

- (UILabel *)typeLabel1
{
    if (!_typeLabel1) {
        _typeLabel1 = [UILabel new];
        _typeLabel1.textColor = Common_Text_TextfieldColor;
        _typeLabel1.textAlignment = NSTextAlignmentCenter;
        _typeLabel1.font = FONT_SIZE(12);
        _typeLabel1.layer.borderWidth = 1;
        _typeLabel1.layer.cornerRadius = 6;
        _typeLabel1.layer.masksToBounds = YES;
        _typeLabel1.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel1;
    
}

//颜色
- (void)resetColorView{
    [self.colorView removeAllSubviews];
    
    CGFloat limitWith = BOUNDWIDTH - 30 - 20;
    __block CGFloat hasWith = 0;
    __block CGFloat hasHeight = 0;
    CGFloat space = 10;
    CGFloat linespace = 4;
    
    @weakify(self);
    [self.model.ysList enumerateObjectsUsingBlock:^(CarConfigurationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        
        UIView *colorbgview = [[UIView alloc]init];
        
        UIView *colorview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
        colorview.backgroundColor = [CommonMethod colorFromHexString:[NSString stringWithFormat:@"#%@",obj.NAME_EN]];
        UILabel *colorTitleLab = [UILabel customInitLabWithFont:10 andTextColor:Common_TextColor andAlign:NSTextAlignmentLeft];
        colorTitleLab.font = [UIFont systemFontOfSize:10];
        colorTitleLab.text = obj.NAME;
        
        colorTitleLab.frame = CGRectMake(14, 0, obj.nameSize.width, 10);
        
        [colorbgview addSubview:colorview];
        [colorbgview addSubview:colorTitleLab];
        
        if ((hasWith + (hasWith == 0?0:space) + obj.nameSize.width + 14) > limitWith) {
            hasWith = 0;
            hasHeight += 10 + linespace;
        }

        colorbgview.frame = CGRectMake(hasWith + (hasWith == 0?10:space), hasHeight + 10, obj.nameSize.width + 14, 10);
        [self.colorView addSubview:colorbgview];
        
        hasWith += CGRectGetWidth(colorbgview.frame) + (hasWith == 0?0:space);
        
    }];
    
    if (self.model.ysList.count == 0) {
        [self.colorView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        [self.colorView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(20 + hasHeight + 10);
        }];
    }
    
}
//安全配置
- (void)resetConfiguration{
    
    
    
    NSMutableArray *allAry = [NSMutableArray arrayWithCapacity:0];
    [allAry addObjectsFromArray:self.model.ssList];
    [allAry addObjectsFromArray:self.model.ckList];
    [allAry addObjectsFromArray:self.model.aqList];
    
    
    
    [self.functionView removeAllSubviews];
//    self.functionView.backgroundColor = Common_BackgroundGrayColor;
    NSMutableArray *functionAry = [NSMutableArray arrayWithCapacity:0];
    [functionAry addObjectsFromArray:self.model.ssList];
    [functionAry addObjectsFromArray:self.model.ckList];
    [functionAry addObjectsFromArray:self.model.aqList];
    //    float count = functionAry.count;
    if (functionAry.count % 2 == 0) {
        
        [self.functionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.colorView.mas_bottom).offset(10);
            make.left.mas_equalTo(15);
            make.height.mas_equalTo(20+20*(functionAry.count/2));
            make.width.mas_equalTo(BOUNDWIDTH - 30);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
        }];
        [self.functionView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];


    } else {
        [self.functionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.colorView.mas_bottom).offset(10);
            make.left.mas_equalTo(15);
            make.height.mas_equalTo(20+20*(functionAry.count/2+1));
            make.width.mas_equalTo(BOUNDWIDTH - 30);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
            
        }];
          [self.functionView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
//        self.functionView.frame = CGRectMake(15, 110, BOUNDWIDTH-30, 20+20*(functionAry.count/2+1));
        
    }
    
    [functionAry enumerateObjectsUsingBlock:^(CarConfigurationModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = functionAry[idx];
        
        UIView *functionBGView = [[UIView alloc] initWithFrame:CGRectMake(0+(BOUNDWIDTH-30)/2*(idx%2), 10+20*(idx/2), (BOUNDWIDTH-30)/2, 15)];
        [self.functionView addSubview:functionBGView];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,(15-4)/2 , 4, 4)];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
        label.backgroundColor = [UIColor blackColor];
        [functionBGView addSubview:label];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, (BOUNDWIDTH-30)/2-20, 15)];
        titleLabel.textColor = RGB(102, 102, 102);
        titleLabel.font = FONT_SIZE(14);
        titleLabel.text = obj.NAME;
        [functionBGView addSubview:titleLabel];
        
    }];
    
//    __block NSString *lefStr = @"";
//    __block NSString *rightStr = @"";
//
//    [allAry enumerateObjectsUsingBlock:^(CarConfigurationModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (idx % 2 == 0) {
//            lefStr = [lefStr stringByAppendingString:[NSString stringWithFormat:@"‧%@\n",obj.NAME]];
//        }else{
//            rightStr = [rightStr stringByAppendingString:[NSString stringWithFormat:@"‧%@\n",obj.NAME]];
//        }
//    }];
//    if (lefStr.length > 0) {
//        lefStr = [lefStr substringWithRange:NSMakeRange(0, lefStr.length - 1)];
//    }
//    if (rightStr.length > 0) {
//        rightStr = [rightStr substringWithRange:NSMakeRange(0, rightStr.length - 1)];
//    }
//
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
//    paragraphStyle.alignment = NSTextAlignmentLeft;  //对齐
//    paragraphStyle.lineSpacing = 10;
//    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
//
//    NSMutableAttributedString *leftAttStr = [[NSMutableAttributedString alloc]initWithString:lefStr attributes:@{
//                                                                                                                 NSFontAttributeName:self.leftLab.font,
//                                                                                                                 NSForegroundColorAttributeName:self.leftLab.textColor,
//                                                                                                                 NSParagraphStyleAttributeName:paragraphStyle
//                                                                                                                 }];
//
//    NSMutableAttributedString *rightAttStr = [[NSMutableAttributedString alloc]initWithString:rightStr attributes:@{
//                                                                                                                 NSFontAttributeName:self.rightLab.font,
//                                                                                                                 NSForegroundColorAttributeName:self.rightLab.textColor,
//                                                                                                                 NSParagraphStyleAttributeName:paragraphStyle
//                                                                                                                 }];
//
//    self.leftLab.attributedText = leftAttStr;
//    self.rightLab.attributedText = rightAttStr;
}






@end
